<template>
  <div>
    <el-table :data="data" border stripe="">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-tag
            v-for="(val, index) in scope.row.attr_vals"
            :key="index"
            closable
            type=""
            @close="tagDelete(index, scope.row)"
            >{{ val }}</el-tag
          >
          <el-button
            size="mini"
            @click="showInput(scope.row)"
            v-if="!scope.row.addNewTagInputVisible"
            class="button-new-tag"
            >+New Tag</el-button
          >
          <el-input
            v-else
            v-model.trim="scope.row.newTagVals"
            @blur="addNewTag(scope.row)"
            @keyup.enter.native="addNewTag(scope.row)"
            class="input-new-tag"
            ref="addNewTagInput"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column
        :label="activeName === 'many' ? '参数名称' : '属性名称'"
        prop="attr_name"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="openEditParamsDialog(scope.row.attr_id)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteParams(scope.row.cat_id, scope.row.attr_id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: 'ParamsTable',
  props: ['data', 'activeName'],
  methods: {
    // 删除属性方法
    async deleteParams(id, attrid) {
      this.$confirm('确定要删除该参数/属性吗？', '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(
            `categories/${id}/attributes/${attrid}`
          )

          if (res.meta.status !== 200) return this.$message.error('删除失败！')

          this.$message.success('删除成功！')
          this.getParamsData()
        })
        .catch((err) => err)
    },
    // 打开编辑对话框方法
    openEditParamsDialog(attrid) {
      // 打开编辑对话框,并把要编辑的数据的id发送到vuex上
      this.$store.commit('params/editDialogVisibleState', true)
      this.$store.commit('params/editDataIdChange', attrid)
    },
    // 添加新标签方法
    async addNewTag(paramsData) {
      // 文本为空则不添加
      // props方式传来的 scope传来的数据是对象,为引用类型,因此修改对象里的属性,不会导致props只读问题
      if (paramsData.newTagVals === '') {
        paramsData.newTagVals = ''
        paramsData.addNewTagInputVisible = false
      } else {
        // console.log(paramsData)
        // paramsData包含添加新标签的所有请求参数
        // 将新标签用push方法添加到被分割后的数组
        paramsData.attr_vals.push(paramsData.newTagVals)
        paramsData.newTagVals = ''
        paramsData.addNewTagInputVisible = false
        // 通过分类id和属性id发起axios添加新标签
        const { data: res } = await this.$http.put(
          `categories/${paramsData.cat_id}/attributes/${paramsData.attr_id}`,
          {
            attr_name: paramsData.attr_name,
            attr_sel: paramsData.attr_sel,
            // 请求体中把被分割的数组在转成字符串上传
            attr_vals: paramsData.attr_vals.toString()
          }
        )
        if (res.meta.status !== 200) {
          return this.$message.error('添加新tag失败！')
        }

        this.$message.success('添加成功！')
      }
    },
    showInput(row) {
      row.addNewTagInputVisible = true
      this.$nextTick(() => {
        this.$refs.addNewTagInput.focus()
      })
    },
    // 删除tag方法
    async tagDelete(tag, paramsData) {
      this.$confirm('确定要删除这个标签属性嘛？', '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // props方式传来的 scope传来的数据是对象,为引用类型,因此修改对象里的属性,不会导致props只读问题
          // tag关闭后传入当前被循环生成的tag的下标，在paramsData.attr_vals删除对应下标的元素
          paramsData.attr_vals.splice(tag, 1)

          const { data: res } = await this.$http.put(
            `categories/${paramsData.cat_id}/attributes/${paramsData.attr_id}`,
            {
              attr_name: paramsData.attr_name,
              attr_sel: paramsData.attr_sel,
              // 请求体中把被分割的数组在转成字符串上传
              attr_vals: paramsData.attr_vals.toString()
            }
          )
          if (res.meta.status !== 200) {
            return this.$message.error('删除tag失败！')
          }

          this.$message.success('删除tag成功！')
        })
        .catch((err) => err)
    }
  }
}
</script>

<style lang="less" scoped>
.el-alert {
  margin-bottom: 15px;
}
.el-tag {
  margin: 5px;
}
.el-row {
  display: flex;
  align-items: center;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
</style>
